package indi.kider.server.woniubus.api;

import indi.kider.common.model.apabus.Destine;
import indi.kider.common.model.apabus.DestineQuery;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.woniubus.DestineTenant;
import indi.kider.common.model.woniubus.TenantVo;
import indi.kider.server.woniubus.service.DestineService;
import indi.kider.server.woniubus.service.related.AuthcService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "destine")
@Log
public class DestineController {


    @Autowired
    private DestineService destineService;
    @Autowired
    AuthcService authcService;

    /**
     * @api {POST} /pageList
     * @apiGroup destine
     * @apiDescription 根据字段查询获取预定列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/destine/pageList
     * {"num":"0","size":"8","query":{housesId:0,destineName:"",destinePhone:"",cardId:"",destineTime:null,eventTime:null,id:0,state:0}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{housesId:0,destineName:"",destinePhone:"",cardId:"",destineTime:null,eventTime:null,id:0,state:0}],"total":1,"size":0}}
     */
    @PostMapping("/pageList")
    public CommonResponse<List<Map>> getDestineList(@RequestBody PageRequest<DestineQuery> request) {
        return CommonResponse.buildGetSuccessResponse(destineService.getDestinePageList(request));
    }

    /**
     * @api {POST} /add
     * @apiGroup destine
     * @apiDescription 预定
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/destine/add
     * {housesId:0,destineName:"",destinePhone:"",cardId:"",destineTime:null,eventTime:null,id:0,state:0}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/add")
    public CommonResponse addDestine(@RequestBody Destine destine) {
        try {
            destineService.destine(destine);
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /update
     * @apiGroup destine
     * @apiDescription 更新预定
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/destine/update
     * {id:,state}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/update")
    public CommonResponse updateDestine(@RequestBody DestineTenant destineTenant) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            destineTenant.getTenantVo().setOpInId(employee.getId());
            destineService.displayDestine(destineTenant.getDestine(),destineTenant.getTenantVo());
            return CommonResponse.buildUpdateSuccessResponse("提交成功");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
